﻿#region Copyright (c) 11/27/2011, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Collections.Concurrent;
using System.Linq;
using NUnit.Framework;


namespace Amarok.Presentation
{
	[TestFixture]
	public class Test_ViewDispatcher
	{
		private class MyViewAction1 : ViewAction
		{
		}

		private class MyViewAction2 : ViewAction
		{
		}

		private class MyViewAction3 : ViewAction
		{
		}

		private class TestClass
		{
			public static ConcurrentQueue<String> Calls = new ConcurrentQueue<String>();

			public static void Reset()
			{
				Calls = new ConcurrentQueue<String>();
			}

			public static Int32 CallCount(String text)
			{
				return Calls.ToArray().Count(c => c.Equals(text));
			}


			[ViewActionHandler]
			private Boolean TestHandler1(MyViewAction1 viewAction)
			{
				Calls.Enqueue("1");
				return false;
			}

			[ViewActionHandler]
			private Boolean TestHandler2(MyViewAction1 viewAction)
			{
				Calls.Enqueue("2");
				return false;
			}

			[ViewActionHandler]
			private Boolean TestHandler3(ViewAction viewAction)
			{
				Calls.Enqueue("3");
				return false;
			}


			[ViewActionHandler]
			private Boolean TestHandler4(MyViewAction2 viewAction)
			{
				Calls.Enqueue("4");
				return true;
			}

			[ViewActionHandler]
			private Boolean TestHandler5(MyViewAction2 viewAction)
			{
				Calls.Enqueue("5");
				return false;
			}

			[ViewActionHandler]
			private Boolean TestHandler6(ViewAction viewAction)
			{
				Calls.Enqueue("6");
				return false;
			}

			[ViewActionHandler]
			private Boolean TestHandler7(MyViewAction2 viewAction)
			{
				Calls.Enqueue("7");
				return false;
			}


			[ViewActionHandler]
			private Boolean TestHandler8(MyViewAction3 viewAction)
			{
				Calls.Enqueue("8");
				return false;
			}

			[ViewActionHandler]
			private Boolean TestHandler9(MyViewAction3 viewAction)
			{
				Calls.Enqueue("9");
				return false;
			}

			[ViewActionHandler]
			private Boolean TestHandler10(MyViewAction3 viewAction)
			{
				Calls.Enqueue("10");
				return false;
			}
		}


		// --- TESTS ---


		[Test]
		public void Test_Invoke_MyViewAction1()
		{
			TestClass.Reset();

			var target = new TestClass();
			var handlerInfos = ViewInspector.Inspect(target.GetType());
			var dispatcher = new ViewDispatcher(handlerInfos, target);

			var result = dispatcher.Invoke(new MyViewAction1());

			Assert.That(result, Is.False);
			Assert.That(TestClass.Calls.Count, Is.EqualTo(4));
			Assert.That(TestClass.CallCount("1"), Is.EqualTo(1));
			Assert.That(TestClass.CallCount("2"), Is.EqualTo(1));
			Assert.That(TestClass.CallCount("3"), Is.EqualTo(1));
			Assert.That(TestClass.CallCount("6"), Is.EqualTo(1));
		}

		[Test]
		public void Test_Invoke_MyViewAction2()
		{
			TestClass.Reset();

			var target = new TestClass();
			var handlerInfos = ViewInspector.Inspect(target.GetType());
			var dispatcher = new ViewDispatcher(handlerInfos, target);

			var result = dispatcher.Invoke(new MyViewAction2());

			Assert.That(result, Is.True);
			Assert.That(TestClass.Calls.Count, Is.EqualTo(2));
			Assert.That(TestClass.CallCount("3"), Is.EqualTo(1));
			Assert.That(TestClass.CallCount("4"), Is.EqualTo(1));
		}

		[Test]
		public void Test_Invoke_MyViewAction3()
		{
			TestClass.Reset();

			var target = new TestClass();
			var handlerInfos = ViewInspector.Inspect(target.GetType());
			var dispatcher = new ViewDispatcher(handlerInfos, target);

			dispatcher.Invoke(new MyViewAction3());

			Assert.That(TestClass.Calls.Count, Is.EqualTo(5));
			Assert.That(TestClass.CallCount("3"), Is.EqualTo(1));
			Assert.That(TestClass.CallCount("6"), Is.EqualTo(1));
			Assert.That(TestClass.CallCount("8"), Is.EqualTo(1));
			Assert.That(TestClass.CallCount("9"), Is.EqualTo(1));
			Assert.That(TestClass.CallCount("10"), Is.EqualTo(1));
		}

		[Test]
		public void Test_Invoke_Null()
		{
			TestClass.Reset();

			var target = new TestClass();
			var handlerInfos = ViewInspector.Inspect(target.GetType());
			var dispatcher = new ViewDispatcher(handlerInfos, target);

			var result = dispatcher.Invoke(null);

			Assert.That(result, Is.False);
			Assert.That(TestClass.Calls.Count, Is.EqualTo(0));
		}

	}
}

#endif
